﻿Imports System.Text.RegularExpressions
Imports System.IO

Public Class TextSerializer
    Shared Function DeserializeDict(f As String) As List(Of DictObject)
        Dim lines() As String = File.ReadAllLines(f)
        Dim l As New List(Of DictObject)
        For Each line As String In lines
            Dim t() As String = Regex.Split(line, vbTab)
            l.Add(New DictObject(t(0), t(1)))
        Next
        DeserializeDict = l
    End Function
    Shared Sub SerializeDict(f As String, l As List(Of DictObject))
        Dim o As New List(Of String)
        For Each dict As DictObject In l
            o.Add(dict.source + vbTab + dict.replacement)
        Next
        File.WriteAllLines(f, o)
    End Sub
    Shared Function DeserializeCoeff(f As String) As List(Of CoeffObject)
        Dim lines() As String = File.ReadAllLines(f, Encoding1251)
        Dim l As New SortedList(Of String, CoeffObject)
        For Each t As String In CacheOperation.types
            l.Add(t, New CoeffObject(t))
        Next
        For Each line As String In lines
            Dim t() As String = Regex.Split(line, vbTab)
            Try
                If (CacheOperation.types.Contains(t(0))) Then

                    Dim c1 As Double = Double.Parse(t(1))
                    Dim c2 As Double = Double.Parse(t(2))
                    Dim c3 As Double = Double.Parse(t(3))
                    Dim t1 As Double = Double.Parse(t(4))
                    Dim t2 As Double = Double.Parse(t(5))
                    With l.Item(t(0))
                        .coeff = c1
                        .coeff2 = c2
                        .coeff3 = c3
                        .thr1 = t1
                        .thr2 = t2
                    End With
                End If
            Catch ex As Exception
            End Try
        Next

        DeserializeCoeff = l.Values.ToList
    End Function
    Shared Sub SerializeCoeff(f As String, l As List(Of CoeffObject))
        Dim o As New List(Of String)
        For Each cf As CoeffObject In l
            With cf
                o.Add(.type + vbTab + _
                      .coeff.ToString + vbTab + .coeff2.ToString + vbTab + .coeff3.ToString + vbTab + _
                      .thr1.ToString + vbTab + .thr2.ToString)
            End With
        Next
        File.WriteAllLines(f, o, Encoding1251)
    End Sub
End Class
